package com.sevencar.activity;

import java.util.ArrayList;

import com.example.lightble.CmdSend;
import com.example.lightble.R;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SelectMoreColorActivity extends Activity implements OnCheckedChangeListener{

	 private CmdSend  mCmdSend;
	 ArrayList<Integer> colorList = new ArrayList<Integer>();  
	 CheckBox ckb;
	 ArrayList<CheckBox> checkBoxList=new ArrayList<CheckBox>();
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.select_more_color);
		mCmdSend=CmdSend.getInstance();
		
		
		setCheckboxClick(R.id.id_select_more_color_beige);
		setCheckboxClick(R.id.id_select_more_color_bisque);
		setCheckboxClick(R.id.id_select_more_color_bluebtn);
		setCheckboxClick(R.id.id_select_more_color_cyan);
		setCheckboxClick(R.id.id_select_more_color_gold);
		setCheckboxClick(R.id.id_select_more_color_greenbtn);
		setCheckboxClick(R.id.id_select_more_color_pink);
		setCheckboxClick(R.id.id_select_more_color_powderblue);
		setCheckboxClick(R.id.id_select_more_color_redbtn);
		setCheckboxClick(R.id.id_select_more_color_skyblue);
		setCheckboxClick(R.id.id_select_more_color_violet);
		setCheckboxClick(R.id.id_select_more_color_writebtn);
		
		
		
		
		findViewById(R.id.id_send_more_color_send).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendColor();
			}
		});
	}
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		 if(isChecked){
			 onClick(buttonView);
		 }
	}
	private void setCheckboxClick(int id)
	{
		CheckBox ckb=(CheckBox)findViewById(id);
		ckb.setOnCheckedChangeListener(this);
		checkBoxList.add(ckb);
	}
	private void onClick(View v) {
		// TODO Auto-generated method stub
		int temp=0;
				switch (v.getId()) {
				case R.id.id_select_more_color_beige:
					 temp=0xFF00FF;
					break;
				case	R.id.id_select_more_color_bisque:
					 temp=0xFFE4C4;
					break;
				case	R.id.id_select_more_color_cyan:
					 temp=0x00FFFF;
					break;
				case 	R.id.id_select_more_color_gold:
					 temp=0xFFD700;
					break;
				case 	R.id.id_select_more_color_pink: 
					 temp=0xD2B48C;
					break;
				case 	R.id.id_select_more_color_powderblue: 
					 temp=0x556B2F;
					break;
				case 	R.id.id_select_more_color_skyblue: 
					 temp=0x87CEEB;
					break;
				case 	R.id.id_select_more_color_violet:
					 temp=0xEE82EE;
					break;
				case R.id.id_select_more_color_redbtn:
					 temp=0xFF0000;
					 break;
				case R.id.id_select_more_color_greenbtn:
					 temp=0x00FF00;
					break;
				case R.id.id_select_more_color_bluebtn:
					 temp=0x0000FF;
					break;
				case R.id.id_select_more_color_writebtn:
					 temp=0xFFFFFF;
					break;
			default:
				break;
			}
		 if(colorList.size()>=10){
			 Toast.makeText(this, "颜色值的个数大于10个,请退出,再次进入后选择.", Toast.LENGTH_SHORT).show();
		 }
		 else
		  colorList.add(temp);
	}
		byte[] test=null;
		private static int  SEND_COUNT=20;
	 private void sendColor()
	 {
		 		Log.i("sno","getMoreRGBArray length22222 is "+ colorList.size());
		     if(colorList.isEmpty()){
		    	 Toast.makeText(this, "没有选择颜色,请选择颜色值.", Toast.LENGTH_SHORT).show();
		    	 return ;
		     }
	    	   test=mCmdSend.getMoreRGBArray(colorList);
	    	   //因为最大是38个, 先这样.
	    	   Log.i("sno","getMoreRGBArray length is "+ test.length);
//	    	   //如果大于等于20 则第一次发送19个字节,第二次发送剩余的字节.
		    	if(test.length>20)
		    	{
		    		byte[] tmp = new byte[SEND_COUNT];
	            	System.arraycopy(test, 0, tmp, 0, SEND_COUNT);

	            	Log.i("sno","getMoreRGBArray  send data-------");	
		    	    HomeFragment.mBLEServiceInstance.sendData(tmp);
		    	    
		    	    
		    	    
		    	    
					new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							//这里没有判断test的长度.
							byte [] tmp2=new byte[test.length-SEND_COUNT];
							System.arraycopy(test, SEND_COUNT, tmp2, 0, test.length-SEND_COUNT);
							HomeFragment.mBLEServiceInstance.sendData(tmp2);
						}
					}, 20);
					
					
					
					
					
					
					
		    	}else{
		    		Log.i("sno","getMoreRGBArray  send data-------");	
		    		HomeFragment.mBLEServiceInstance.sendData(test);
		    	}
		    	 Toast.makeText(this, colorList.size()+" 组颜色值ONE已发送...", Toast.LENGTH_SHORT).show();
		    	colorList.clear();
		    	for (CheckBox ckb : checkBoxList) {
		    		ckb.setChecked(false);
				}

		    	
	 }

}
